<?

function valid_email($email) {
	  // Frst, we check that there's one @ symbol, and that the lengths are right
	if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
		// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
		return false;
	}
	  // Split it into sections to make life easier
	$email_array = explode("@", $email);
	$local_array = explode(".", $email_array[0]);
	for ($i = 0; $i < sizeof($local_array); $i++) {
		if (!ereg("^(([A-Za-z0-9!#$%&#038;'*+/=?^_`{|}~-][A-Za-z0-9!#$%&#038;'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
		return false;
		}
	}  
	if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
		$domain_array = explode(".", $email_array[1]);
		if (sizeof($domain_array) < 2) {
		return false; // Not enough parts to domain
		}
		for ($i = 0; $i < sizeof($domain_array); $i++) {
		if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
			return false;
		}
		} 
	}
	return true;
}

// *************************
// Random Password Generator
// *************************
function password_generator($totalChar){
	//    $salt = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789";  // salt to select chars from
	$salt = "abcdefghijklmnpqrstuvwxyz123456789";  // salt to select chars from
	srand((double)microtime()*1000000); // start the random generator
	$password=""; // set the inital variable
	for ($i=0;$i<$totalChar;$i++)  // loop and create password
	  $password = $password . substr ($salt, rand() % strlen($salt), 1);
	return $password;
}

function SidToString($bin){
	$check1 = unpack("C", substr($bin, 0, 1));
	if ($check1[1] != 1) {return 0;}

	$check2 = unpack("C", substr($bin, 1, 1));
	if (strlen($bin) != 8 + 4 * $check2[1]){ return 0; }
	$sid_str = "S-1-";
	$part1 = unpack("C", substr($bin, 7, 1));
	$part2 = unpack("C", substr($bin, 6, 1));
	$part3 = unpack("C", substr($bin, 5, 1));
	$part4 = unpack("C", substr($bin, 4, 1));
	$sid_str .= $part1[1] + ($part2[1] << 8) + ($part3[1] << 16) + ($part4[1] << 24);

	$count = unpack("C", substr($bin, 1, 1));
	$count = $count[1];
	for ($loop = 0; $loop<$count; $loop++){
		$part = unpack("S", substr($bin, 4 * $loop + 8, 4));
	  $sid_str .= "-" .$part[1];
	  echo $i;
	}
	return $sid_str;
}
?>
